package com.movie.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.movie.api.mapper.UserIsVipMapper;
import com.movie.api.mapper.UserMapper;
import com.movie.api.model.entity.User;
import com.movie.api.model.entity.UserVip;
import com.movie.api.service.UserIsVipService;
import com.movie.api.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class UserIsVipServiceImpl extends ServiceImpl<UserIsVipMapper, UserVip> implements UserIsVipService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserService userService;
    @Resource
    private UserIsVipMapper userIsVipMapper;

    public boolean isVip(String username) {
        log.info(username);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);
        User currentuser = userMapper.selectOne(userQueryWrapper);
        log.info(String.valueOf(currentuser));
        String userId = currentuser.getId();
        log.info("userId为：{}", userId);
        UserVip currentUserVip = this.lambdaQuery()
                .eq(UserVip::getUserId, userId)
                .one();
        log.info("currentUserVip为：{}", currentUserVip);
        return currentUserVip.getIsVip() == 1;

    }

    @Override
    public void setVipById(String strId) {
        QueryWrapper<UserVip> userVipQueryWrapper = new QueryWrapper<>();
        userVipQueryWrapper.eq("user_id", strId);
        UserVip userVip = userIsVipMapper.selectOne(userVipQueryWrapper);
        log.info("要设置成VIP的user：{}", userVip);
        userVip.setIsVip(1);
        int result = userIsVipMapper.update(userVip, userVipQueryWrapper);
        log.info("设置VIP成功！用户：{}成为VIP",userVip.getUserId());
//        if (result == 1) {
//            log.info("设置VIP成功！用户：{}成为VIP",userVip.getUserId());
//            return true;
//        }else {
//            return false;
//        }
    }

}